$(function () {
    $("#jqGrid").jqGrid({
        treeGrid: true,
        treeGridModel: 'adjacency', //treeGrid模式，跟json元数据有关 ,adjacency/nested
        ExpandColumn: 'id',
        url: '../sys/menu/list',
        datatype: "json",
        colModel: [
            {label: '菜单ID', name: 'menuId', width: 40, key: true},
            {label: '菜单名称', name: 'name', width: 60},
            {label: '上级菜单', name: 'parentName', width: 60},
            {
                label: '菜单图标', name: 'icon', width: 50, formatter: function (value, options, row) {
                    return value == null ? '' : '<i class="' + value + ' fa-lg"></i>';
                }
            },
            {label: '菜单URL', name: 'url', width: 100},
            {label: '授权标识', name: 'perms', width: 100},
            {
                label: '类型', name: 'type', width: 50, formatter: function (value, options, row) {
                    if (value === 0) {
                        return '<span class="label label-primary">目录</span>';
                    }
                    if (value === 1) {
                        return '<span class="label label-success">菜单</span>';
                    }
                    if (value === 2) {
                        return '<span class="label label-warning">按钮</span>';
                    }
                }
            },
            {label: '排序号', name: 'orderNum', width: 50}
        ],
        viewrecords: true,
        height: 800,
        // rowNum: 20,
        // rowList : [20,30,50],
        // rownumbers: true,
        // rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        jsonReader: {
            root: "resData"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        treeReader: {
            level_field: "level",
            parent_id_field: "parentId",
            leaf_field: "isLeaf",
            expanded_field: "open"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        isShowCond: false,
        btnText: '查询',
        qryForm: {}
    },
    methods: {
        reset: function () {
            this.qryForm.paramKey = '';
        },
        query: function () {
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {},
                page: 1
            }).trigger("reloadGrid");
        },
        toggle: function () {
            this.isShowCond = !this.isShowCond;

            if (this.isShowCond == true) {
                this.btnText = "隐藏"
            } else if (this.isShowCond == false) {
                this.btnText = "查询"
            }

        },
        create: function (event) {
            var grid = $("#jqGrid");
            var rowKey = grid.getGridParam("selrow");
            if (!rowKey) {
                location.href = "menu_add.html";
            } else {
                location.href = "menu_add.html?parentId=" + rowKey;
            }
        },
        update: function (event) {
            var menuId = getSelectedRow();
            if (menuId == null) {
                return;
            }

            location.href = "menu_add.html?menuId=" + menuId;
        },
        del: function (event) {
            var menuId = getSelectedRow();
            if (menuId == null) {
                return;
            }
            var menuIds = [menuId];
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../sys/menu/delete",
                    data: JSON.stringify(menuIds),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        }
    }
});